<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>模糊搜索</title>
    <script src="../js/vue.js"></script>
</head>

<body>
    <!-- 
        当监听属性watch和计算属性computed都能实现需求时，首选计算属性computed
     -->
    <div id="app">
        <h1>列表模糊搜索</h1>
        <input type="text" placeholder="请输入名字" v-model="keyWord">
        <ul>
            <li v-for="(p,index) in filPersons" :key="p.id"> {{p.name}}-{{p.age}} </li>
        </ul>
    </div>
    <script>
        Vue.config.productionTip = false
        //watch数据监视去实现
        //#region
        /*const vm = new Vue({
            data() {
                return {
                    keyWord: '',
                    persons: [
                        { id: '001', name: '周冬雨', age: 18 },
                        { id: '002', name: '马冬梅', age: 19 },
                        { id: '003', name: '周杰伦', age: 20 },
                        { id: '004', name: '温兆伦', age: 20 }
                    ],
                    filPersons: []
                }
            },
            watch: {
                keyWord: {
                    immediate: true,
                    handler(newVal) {
                        console.log('keyWord被修改了', newVal)
                        this.filPersons = this.persons.filter((p) => {
                            return p.name.indexOf(newVal) !== -1
                        })
                    }
                }
            }
            
        })*/
        //#endregion

        //用computed实现
        const vm = new Vue({
            data() {
                return {
                    keyWord: '',
                    persons: [
                        { id: '001', name: '周冬雨', age: 18 },
                        { id: '002', name: '马冬梅', age: 19 },
                        { id: '003', name: '周杰伦', age: 20 },
                        { id: '004', name: '温兆伦', age: 20 }
                    ]
                }
            },
            computed: {
                filPersons() {
                    console.log("调用了没")
                    return this.persons.filter((p) => {
                        return p.name.indexOf(this.keyWord) !== -1
                    })
                }
            }

        })

        vm.$mount('#app')
    </script>
</body>

</html>